package com.jinbo.footmessage;


import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionResult;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.jinbo.footmessage.utils.BaiduMapUtils;
import com.jinbo.footmessage.utils.LatLngBean;
import com.jinbo.footmessage.utils.MyOverlay;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jinbo on 2015/8/12.
 */
public class MyBaiduListenerHelper implements BDLocationListener,OnGetGeoCoderResultListener
,BaiduMap.OnMapStatusChangeListener,OnGetSuggestionResultListener,BaiduMap.OnMarkerClickListener {//,OnGetPoiSearchResultListener
    private OnMapChangeListener onMapChangeListener;
    private OnLocationListener onLocationListener;
    private OnGeoListener onGeoListener;
   // private OnGetPoiListener onGetPoiListener;
    private OnGetSuggestionListener onGetSuggestionListener;
    private OnGetMarkClickListener onGetMarkClickListener;
    private BaiduMap mBaiduMap;
    private LocationClient mLocationClient;
    private GeoCoder coder;
    //private PoiSearch mPoiSearch;
    private SuggestionSearch mSuggestionSearch;




    public MyBaiduListenerHelper(BaiduMapUtils mMapUntil)
    {
        mBaiduMap = mMapUntil.getMap();
        mLocationClient=mMapUntil.getLocationClient();
        coder= mMapUntil.getGeoCoder();
       // mPoiSearch=mMapUntil.getPoiSearch();
        mSuggestionSearch=mMapUntil.getSuggestionSearch();
}


    public interface OnGetMarkClickListener {
        void onMarkerClick(MyOverlay overlay);
    }

    public void setMarkClickListener(OnGetMarkClickListener onGetMarkClickListener) {
        this.onGetMarkClickListener = onGetMarkClickListener;
        mBaiduMap.setOnMarkerClickListener(this);
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        MyOverlay overlay=new MyOverlay(marker);
        onGetMarkClickListener.onMarkerClick(overlay);
        return false;
    }




    public interface OnGetSuggestionListener {
        void onGetSuggestionResult(ArrayList<String> addressName,
                                   ArrayList<String> addressDistrict, ArrayList<LatLngBean> lls);//,ArrayList<String> cities);
    }

    public void setGetSuggestionListener(OnGetSuggestionListener onGetSuggestionListener) {
        this.onGetSuggestionListener =onGetSuggestionListener;
        mSuggestionSearch.setOnGetSuggestionResultListener(this);
    }


    @Override
    public void onGetSuggestionResult(SuggestionResult res) {
        ArrayList<String> song_name=new ArrayList<String>();
        ArrayList<String> song_singer=new ArrayList<String>();
        ArrayList<LatLngBean> points=new ArrayList<LatLngBean>();
        if (res == null || res.getAllSuggestions() == null||res.getAllSuggestions().size()==0) {
            //song_name=null;
           // song_singer=null;
           // points=null;
        }
        else {
            //ArrayList<String> cities=new ArrayList<String>();
            for (SuggestionResult.SuggestionInfo info : res.getAllSuggestions()) {
                if (info.key != null && info.pt != null && (!info.city.equals("") || !info.district.equals(""))) {
                    LatLngBean bean = new LatLngBean();
                    bean.setLatitude(info.pt.latitude);
                    bean.setLongitude(info.pt.longitude);

                    points.add(bean);
                    //cities.add(info.city);
                    song_name.add(info.key);
                    song_singer.add(info.city + info.district);
                }
            }
        }


        onGetSuggestionListener.onGetSuggestionResult(song_name, song_singer, points);//,cities);
    }

/*
    public interface OnGetPoiListener {
        void onGetPoiResult();
        void onGetPoiDetailResult();
    }

    public void setGetPoiListener(OnGetPoiListener onGetPoiListener) {
        this.onGetPoiListener = onGetPoiListener;
        mPoiSearch.setOnGetPoiSearchResultListener(this);
    }


    @Override
    public void onGetPoiResult(PoiResult poiResult) {

    }

    @Override
    public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

    }

*/
    public interface OnMapChangeListener {
       void mapChangeStart();
       void mapChangeFinish(LatLngBean code);
    }

    public void setMapChangeListener(OnMapChangeListener onMapChangeListener) {
        this.onMapChangeListener = onMapChangeListener;
        mBaiduMap.setOnMapStatusChangeListener(this);

    }


    @Override
    public void onMapStatusChange(MapStatus arg0) {
        Log.d("status", "change");
    }

    /**
     * 地图状态变化结束
     *
     * @param arg0 当前地图状态
     */

    @Override
    public void onMapStatusChangeFinish(MapStatus arg0) {
        Log.d("status", "finish");
        LatLng code=arg0.target;
        LatLngBean bean =new LatLngBean();
        bean.setLatitude(code.latitude);
        bean.setLongitude(code.longitude);
        onMapChangeListener.mapChangeFinish(bean);

    }

    /**
     * 地图状态改变开始
     *
     * @param arg0 地图状态改变结束后的地图状态
     */


    @Override
    public void onMapStatusChangeStart(MapStatus arg0) {
        Log.d("status", "start");
        onMapChangeListener.mapChangeStart();
    }




    public interface OnLocationListener {
         void onLocation(double latitude, double Longitude);
    }


    public void setLocationListener(OnLocationListener  onLocationListener) {
        this.onLocationListener = onLocationListener;
        mLocationClient.registerLocationListener(this);
    }


    @Override
    public void onReceiveLocation(BDLocation location) {
        if (location == null) {
            return;
        }
        double latitude=location.getLatitude();
        double Longitude=location.getLongitude();
        onLocationListener.onLocation(latitude, Longitude);

    }




    public interface OnGeoListener {
         void onReverseGeo(int isError, ArrayList<String> addressName,
                           ArrayList<String> addressDistrict, ArrayList<LatLngBean> lls, String city, String province);
         void onGeo(Boolean isError, LatLngBean bean);//,ArrayList<String> addressCity
    }

    public void setGeoListener(OnGeoListener  onGeoListener) {
        this.onGeoListener = onGeoListener;
        coder.setOnGetGeoCodeResultListener(this);
    }



    @Override
    public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
        int isError=0;
        if(result == null)
            isError=1;
        if( result.error != SearchResult.ERRORNO.NO_ERROR)
            isError=2;
       // ArrayList<String> addressCity=new ArrayList<String>();
        ArrayList<String> addressName=new ArrayList<String>();
        ArrayList<String> addressDistrict=new ArrayList<String>();
        ArrayList<LatLngBean> lls=new ArrayList<LatLngBean>();//用于用户选择周边后，把坐标传到这个列表里面
        String city=null;
        String province=null;
        if(isError==0) {
            //addressName.clear();
            //addressDistrict.clear();
           // lls.clear();
            province=result.getAddressDetail().province;
            city=result.getAddressDetail().city;
            //addressCity.add(city);
            addressName.add("[当前位置]");
            addressDistrict.add(result.getAddress());//ad.province+ad.city+ad.district+ad.street+ad.streetNumber);
            LatLngBean bean1 =new LatLngBean();
            bean1.setLatitude(result.getLocation().latitude);//mBaiduMap.getMapStatus().target.latitude);
            bean1.setLongitude(result.getLocation().longitude);//mBaiduMap.getMapStatus().target.longitude);
            lls.add(bean1);
            List<PoiInfo> listInf = result.getPoiList();// POI数据
            if (listInf != null) {
                for (int i = 0; i < listInf.size(); i++) {
                    PoiInfo p = listInf.get(i);
                 //   addressCity.add(p.city);
                   // Log.d("city",p.city+"!!!!"+p.name+"    ");
                    addressName.add(p.name);
                    addressDistrict.add(p.address);
                    LatLngBean bean2=new LatLngBean();
                    bean2.setLatitude(p.location.latitude);
                    bean2.setLongitude(p.location.longitude);
                    lls.add(bean2);
                }
            }
        }
        onGeoListener.onReverseGeo(isError,addressName,
                addressDistrict, lls,city,province);//,addressCity,
    }


    @Override
    public void onGetGeoCodeResult(GeoCodeResult result) {
        Boolean isError=(result == null || result.error != SearchResult.ERRORNO.NO_ERROR);
        LatLngBean bean = new LatLngBean();
        if(!isError) {
            LatLng ll = result.getLocation();
            bean.setLatitude(ll.latitude);
            bean.setLongitude(ll.longitude);
        }
        onGeoListener.onGeo(isError,bean);
    }

}
